/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.*;
import java.text.*;
import java.text.NumberFormat;
import Models.HoaDon_Model;
import Models.PhieuDatMA_Model;
import Models.KhachHang_Model;
import Models.Ban_Model;
import Models.NhatKyHT_Model;
import Beans.NhanVien;
import Models.DoanhNghiep_Model;
import Models.NhanVien_Model;

/**
 *
 * @author Venus
 */
public class PhieuDatMA_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");            
            if (action.equals("Show")) {
                String maquyen = "48";
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                if (NhanVien_Model.CheckQuyen(Integer.parseInt(maquyen), nv.getMaNV())) {
                    PhieuDatMA_Model m = new PhieuDatMA_Model();
                    Vector v = new Vector();
                    v = m.LayDSBanTinhTien();
                    request.setAttribute("DSBan", v);
                    RequestDispatcher rd = request.getRequestDispatcher("cashier/phieudatmonan.jsp");
                    rd.forward(request, response);
                }
                else{
                    response.sendRedirect("index.jsp");
                }
            }
            else if (action.equals("ShowOrderbill")) {
                PhieuDatMA_Model m = new PhieuDatMA_Model();
                Vector v = new Vector();
                v = m.LayDSBanTinhTien();
                request.setAttribute("DSBan", v);
                RequestDispatcher rd = request.getRequestDispatcher("cashier/phieudma.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("Detail")){
                String maban = request.getParameter("maban");
                PhieuDatMA_Model pdmam = new PhieuDatMA_Model();
                Vector v = new Vector();
                v = pdmam.Detail(Integer.parseInt(maban));
                int sohdt = PhieuDatMA_Model.LaySoHDT(Integer.parseInt(maban));
                Vector vCT = new Vector();
                vCT = pdmam.DetailPDMA(sohdt);                
                request.setAttribute("PDMA", v);
                request.setAttribute("Detail", vCT);
                RequestDispatcher rd = request.getRequestDispatcher("cashier/detailpdma.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("UpdatePDMA")){
                String makh = request.getParameter("makh");
                String sodht = request.getParameter("sohdt");
                String maban = request.getParameter("maban");
                if(makh.equals("") || makh.equals("0")){
                    int ChietKhau = 0;
                    int makhachhang = 0;
                    PhieuDatMA_Model.UpdatePDMA(makhachhang, ChietKhau, Integer.parseInt(sodht));
                }
                else{
                    int ChietKhau = PhieuDatMA_Model.LayChietKhau(Integer.parseInt(makh));
                    PhieuDatMA_Model.UpdatePDMA(Integer.parseInt(makh), ChietKhau, Integer.parseInt(sodht));
                }
                PhieuDatMA_Model pdmam = new PhieuDatMA_Model();
                Vector v = new Vector();
                v = pdmam.Detail(Integer.parseInt(maban));
                int sohdt = PhieuDatMA_Model.LaySoHDT(Integer.parseInt(maban));
                Vector vCT = new Vector();
                vCT = pdmam.DetailPDMA(sohdt);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "orderbill");
                request.setAttribute("PDMA", v);
                request.setAttribute("Detail", vCT);
                RequestDispatcher rd = request.getRequestDispatcher("cashier/detailpdma.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("CheckKHExist")){
                String makh = request.getParameter("makh");
                if(makh.equals("") || makh.equals("0")){
                    out.print("true");
                }
                else{
                    if(KhachHang_Model.CheckKHExist(Integer.parseInt(makh))){
                        out.print("true");
                    }
                    else{
                        out.print("false");
                    }
                }                
            }
            else if(action.equals("Notification")){
                int count = PhieuDatMA_Model.Notification();
                if(count != 0){
                    out.print("<a href='../PhieuDatMA_Controller?action=Show&item=1' style='color: #fff; text-decoration: none; '><div class = 'checkk'>"+count+"</div></a>");
                }
                else{
                    out.print("<img style=' padding-top: 10px; margin-left: 30px; margin-top: 3px; margin-bottom: 8px;' alt='' src='../images/notification.png'>");
                }
            }
            else if(action.equals("Notification1")){
                int count = PhieuDatMA_Model.Notification();
                if(count != 0){
                    out.print("<a href='PhieuDatMA_Controller?action=Show&item=1' style='color: #fff; text-decoration: none; '><div class = 'checkk'>"+count+"</div></a>");
                }
                else{
                    out.print("<img style=' padding-top: 10px; margin-left: 30px; margin-top: 3px; margin-bottom: 8px;' alt='' src='images/notification.png'>");
                }
            }
            else if(action.equals("CheckTien")){
               String tamtinh = request.getParameter("tamtinh");
               String tienkhach = request.getParameter("tienkhach");
               double tt = Double.parseDouble(tamtinh);
               double tk = Double.parseDouble(tienkhach);
               int i = Double.compare(tk, tt);
               if(i < 0)
               {
                    out.print("false");
               }
               else{
                   out.print("true");
               }
            }
            else if(action.equals("InHoaDon")){
                String sohdt = request.getParameter("sohdt");
                String tienkhach = request.getParameter("tienkhach");
                String maban = request.getParameter("maban");
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                int MaNVTN = nv.getMaNV();
                PhieuDatMA_Model.InHoaDon(sohdt, tienkhach , MaNVTN);
                PhieuDatMA_Model.ChiTietHoaDon(sohdt);                
                PhieuDatMA_Model.XoaPDMA(sohdt);
                PhieuDatMA_Model.XoaCTPDMA(sohdt);
                Ban_Model.UpdateTTBan("4", maban);
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "bill");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.Bill(Integer.parseInt(sohdt));
                Vector vCT = new Vector();
                vCT = hdm.DetailBill(Integer.parseInt(sohdt));
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                String ngaygio = fm.format(cal.getTime());
                request.setAttribute("PDMA", v);
                request.setAttribute("Detail", vCT);
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("ngaygio", ngaygio);
                RequestDispatcher rd = request.getRequestDispatcher("cashier/printbill.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("InPDMA")){
                String sohdt = request.getParameter("sohdt");
                String maban = request.getParameter("maban");
                PhieuDatMA_Model pdmam = new PhieuDatMA_Model();
                Vector v = new Vector();
                v = pdmam.PDMA(Integer.parseInt(sohdt));
                Vector vCT = new Vector();
                vCT = pdmam.DetailPDMA(Integer.parseInt(sohdt));
                Ban_Model.UpdateTTBan("3", maban);
                String tendn = DoanhNghiep_Model.TenDN();
                String diachi = DoanhNghiep_Model.DiaChi();
                String sodt = DoanhNghiep_Model.SoDT();                
                request.setAttribute("tendn", tendn);
                request.setAttribute("diachi", diachi);
                request.setAttribute("sodt", sodt);
                request.setAttribute("PDMA", v);
                request.setAttribute("Detail", vCT);
                
                RequestDispatcher rd = request.getRequestDispatcher("cashier/printpdma.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("LayTienThoiLai")){
                String tienkhach = request.getParameter("tienkhach");
                String tamtinh = request.getParameter("tamtinh");
                double tt = Double.parseDouble(tamtinh);
                double tk = Double.parseDouble(tienkhach);
                double tienthoilai = tk - tt;
                double rs = PhieuDatMA_Model.Round(tienthoilai, 2);
                NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
                out.print(nf.format(rs));
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
